home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / wdj0697.zip / WIESMAN.ZIP / CLOCKCTL.CPP < prev    next >
C/C++ Source or Header  |  1997-01-20  |  2KB  |  99 lines

  1. // ClockCtl.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "CustCtrl.h"
  6. #include "ClockCtl.h"
  7. #include "math.h"
  8.  
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CClockControl
  17.  
  18. CClockControl::CClockControl()
  19. {
  20.     m_nMinutes = 0;
  21.     m_nHours = 0;
  22.     m_nSeconds = 0;
  23. }
  24.  
  25. CClockControl::~CClockControl()
  26. {
  27. }
  28.  
  29.  
  30. BEGIN_MESSAGE_MAP(CClockControl, CWnd)
  31.     //{{AFX_MSG_MAP(CClockControl)
  32.     ON_WM_PAINT()
  33.     //}}AFX_MSG_MAP
  34. END_MESSAGE_MAP()
  35.  
  36.  
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CClockControl message handlers
  39.  
  40. void CClockControl::OnPaint() 
  41. {
  42.     CPaintDC dc(this); // device context for painting
  43.     
  44.     CRect rect;
  45.     GetClientRect(&rect);
  46.     CPoint ptCenter = rect.CenterPoint();
  47.  
  48.     CDC dcMem, dcTemp;
  49.     dcTemp.CreateCompatibleDC(&dc);    // temp dc for rendering
  50.     CBitmap bitmap, *pTempOld;
  51.     bitmap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
  52.     pTempOld = dcTemp.SelectObject(&bitmap);
  53.     // now do rendering
  54.     CBrush brush(GetSysColor(COLOR_BTNFACE));
  55.     CPen *pOldPen, penRed(PS_SOLID, 1, RGB(255, 0, 0));
  56.     CPen penThick(PS_SOLID, 2, RGB(0, 0, 0));
  57.     dcTemp.FillRect(&rect, &brush);
  58.  
  59.     int nMaxLength = min(rect.Width(), rect.Height()) / 2 - 5;
  60.     CPoint pt;
  61.     pt = GetHandPos(nMaxLength * 5 / 10, m_nHours * 30 + m_nMinutes / 2);
  62.     pOldPen = dcTemp.SelectObject(&penThick);
  63.     dcTemp.MoveTo(ptCenter);
  64.     dcTemp.LineTo(ptCenter + pt);
  65.  
  66.     pt = GetHandPos(nMaxLength * 8 / 10, m_nMinutes * 6);
  67.     dcTemp.MoveTo(ptCenter);
  68.     dcTemp.LineTo(ptCenter + pt); 
  69.  
  70.     pt = GetHandPos(nMaxLength * 8 / 10 + 5, m_nSeconds * 6);
  71.     dcTemp.SelectObject(&penRed);
  72.     dcTemp.MoveTo(ptCenter);
  73.     dcTemp.LineTo(ptCenter + pt);
  74.     dcTemp.SelectObject(pOldPen);
  75.  
  76.     // now back to the real dc
  77.     dc.BitBlt(0, 0, rect.Width(), rect.Height(), &dcTemp, 0, 0, SRCCOPY);
  78.     dcTemp.SelectObject(pTempOld);
  79. }
  80.  
  81. CPoint CClockControl::GetHandPos(int nLen, int nDegrees)
  82. {
  83.     double pi = 3.1415926535;
  84.     double radians = (2 * pi * (double)(360 - nDegrees + 90) / 360.0);
  85.     int x = (int)(cos(radians) * nLen);
  86.     int y = -(int)(sin(radians) * nLen);
  87.  
  88.     return CPoint(x, y);
  89. }
  90.  
  91. void CClockControl::SetToCurrentTime()
  92. {
  93.     CTime timeNow = CTime::GetCurrentTime();
  94.  
  95.     m_nHours = (short)(timeNow.GetHour() % 12);
  96.     m_nMinutes = (short)timeNow.GetMinute();
  97.     m_nSeconds = (short)timeNow.GetSecond();
  98. }
  99.